print("调用函数##################################################start")
# 求绝对值的函数
print(abs(-100))
# 可以接收任意多个参数，并返回最大的那个
print(max(1, 8, 9, 12, 15, 18))
# 数据类型转换
print(int('123'))
print(float('12.34'))
print(str(1.23))
print(str(100))
print(bool(1))
print(bool(''))
print("调用函数##################################################end\n")

print("定义函数##################################################start")


# 自定义获取绝对值的函数
def my_abs(x):
    # 如果没有return语句，函数执行完毕后也会返回结果，只是结果为None。return None可以简写为return
    if x > 0:
        return x
    else:
        return -x


print(my_abs(-100))


# 定义空函数
def nop():
    # 如果想定义一个什么事也不做的空函数，可以用pass语句,缺少了pass，代码运行就会有语法错误
    pass


print(nop())


# 定义执行参数检查函数
def my_abs_2(x):
    if not isinstance(x, (int, float)):
        raise TypeError("参数数据类型不正确")


# print(my_abs_2("2"))

# 定义返回多个值的方法与默认参数，实际上还是一个值，只不过把多个结果组装成了tuple
import math


def move(x, y, step, angle=0):
    nx = x + step * math.cos(angle)
    ny = y + step * math.sin(angle)
    return nx, ny


x, y = move(100, 100, 60)
print(x)
print(y)
r = x, y
print(r)
r = move(100, 100, 60, 100 / 6)
print(r)


# 定义多个默认参数的函数
def enroll(name, gender, age=6, city='Beijing'):
    print(name)
    print(gender)
    print(age)
    print(city)


enroll("wx", "男")
enroll("wx", "男", 23)
enroll("wx", "男", city="南昌")


# 默认参数必须指向不变对象！
def add_end(L=[]):
    L.append("END")
    return L


print(add_end([1, 2, 3, 4]))
print(add_end())
print(add_end())


def add_end2(L=None):
    if L is None:
        L = []
    L.append("END")
    return L


print(add_end2())
print(add_end2())


# 可变参数
def calc(*numbers):
    sum = 0
    for i in numbers:
        sum = sum + i
    return sum


print(calc(1, 2, 3, 4))
print(calc(*(1, 2, 3, 4, 5)))
print(calc(*[1, 2, 3, 4, 5]))
print(calc(*range(10)))


# 设置关键字参数
def person(name, age, **kw):
    print('name:', name, 'age:', age, 'other:', kw)


person("wx", 18)
person("wx", 18, city="南昌")
extra = {'city': 'Beijing', 'job': 'Engineer'}
person("wx", 18, **extra)


# 设置限制关键字参数的名字（命名关键字参数）
def person2(name, age, *, city="南昌", job):
    print(name, age, city, job)


person2('Jack', 24, job='Engineer')
person2('Jack', 24, city='Beijing', job='Engineer')


# 参数组合 数定义的顺序必须是：必选参数、默认参数、可变参数、命名关键字参数和关键字参数。
def f1(a, b, c=0, *args, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)


def f2(a, b, c=0, *, d, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)


args = (1, 2, 3, 4)
kw = {'d': 99, 'x': '#'}
f1(*args, **kw)

args = (1, 2, 3)
kw = {'d': 88, 'x': '#'}
f2(*args, **kw)


# 递归函数
def fact(n):
    if n == 1:
        return 1
    return n * fact(n - 1)


print(fact(5))
print("定义函数##################################################end\n")
